package com.wenxixuanwu.fruitstore.service.impl;

import com.wenxixuanwu.fruitstore.entity.Category;
import com.wenxixuanwu.fruitstore.mapper.BackCategoryMapper;
import com.wenxixuanwu.fruitstore.service.BackCategoryService;
import com.wenxixuanwu.fruitstore.service.exception.ProductNotFundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author :小凡凡
 * @version :1.0
 * @Project :fruitstore
 * @Date :2022/4/26
 */
@Service
public class BackCategoryServiceImpl implements BackCategoryService {

    @Autowired
    private BackCategoryMapper categoryMapper;

    @Override
    public List<Category> findCategoryAll() {
        List<Category> result = categoryMapper.findCategoryAll();
        if (result == null){
            throw new ProductNotFundException("商品分类的数据未找到！");
        }
        return result;
    }

    @Override
    @Transactional
    public Integer insertCategory(String category) {
        Integer result = categoryMapper.insertCategory(category);
        if (result != 1){
            throw  new ProductNotFundException("商品类别插入失败！");
        }
        return result;
    }

    @Override
    public Integer deleteCategory(Integer id) {
        Integer result = categoryMapper.deleteCategory(id);
        if (result != 1){
            throw new ProductNotFundException("删除商品类型失败，请联系管理员！");
        }
        return result;
    }
}
